package io.github.edwinliu.anynet;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.util.Log;

import java.util.HashMap;

public class ProviderUtils {
    public final static String TAG = "ProviderUtils";
    private ContentResolver contentResolver;
    private static HashMap<String, String> maps;
    private static ProviderUtils instance;

    public final static String KEY_NAME = "setName";
    public final static String KEY_VALUE = "setValue";
    public final static String KEY_NAME_VPN = "vpn";
    public final static String KEY_NAME_NETTYPE = "nettype";

    public static ProviderUtils getInstance(Context paramContext) {
        if (null == instance) {
            synchronized (ProviderUtils.class) {
                if (null == instance) {
                    instance = new ProviderUtils(paramContext);
                }
            }
        }
        maps = loadData(paramContext);
        return instance;
    }

    public ProviderUtils(Context paramContext) {
        contentResolver = paramContext.getContentResolver();
    }

    public HashMap<String, String> getMaps() {
        return maps;
    }


    public float getFloatValue(String key) {
        return TextUtils.isEmpty(maps.get(key)) ? 0 : Float.parseFloat(maps.get(key));
    }

    public double getDoubleValue(String key) {
        return TextUtils.isEmpty(maps.get(key)) ? 0 : Double.parseDouble(maps.get(key));
    }

    public boolean getBooleanValue(String key) {
        boolean ret=!TextUtils.isEmpty(maps.get(key)) && Boolean.parseBoolean(maps.get(key));
        Log.d(TAG,"getBooleanValue "+key+"="+ret);
        return ret;
    }

    public String getValue(String key) {
        return TextUtils.isEmpty(maps.get(key)) ? "" : maps.get(key);
    }

    public int getIntValue(String key) {
        int ret=TextUtils.isEmpty(maps.get(key)) ? 0 : Integer.parseInt(maps.get(key));
        Log.d(TAG,"getIntValue "+key+"="+ret);
        return ret;
    }

    public void setValue(String keyName, String keyValue) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_VALUE, keyValue);
        Log.d(TAG,"setValue "+keyName+"="+keyValue);
        if (null == maps.get(keyName)) {
            contentValues.put(KEY_NAME, keyName);
            contentResolver.insert(NetTypeProvider.NETTYPE_CONTENT_URI, contentValues);
        } else {
            contentResolver.update(NetTypeProvider.NETTYPE_CONTENT_URI, contentValues,
                    KEY_NAME + "=?", new String[]{keyName});
        }

        maps.put(keyName, keyValue);
    }


    public static HashMap<String, String> loadData(Context context) {
        HashMap<String, String> map = new HashMap<>();
        Cursor cursor = null;
        try {
            ContentResolver contentResolver = context.getContentResolver();
            cursor = contentResolver.query(NetTypeProvider.NETTYPE_CONTENT_URI,
                    new String[]{KEY_NAME, KEY_VALUE},
                    null, null, null, null);
            if (cursor == null) {
                return map;
            }
            while (cursor.moveToNext()) {
                String key = cursor.getString(0);
                String value = cursor.getString(1);
                map.put(key, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return map;
    }
}
